package br.com.dreamsource.mobile.worders.forms.customer;

import br.com.dreamsource.mobile.components.DBChoiceGroup;
import br.com.dreamsource.mobile.components.DBTextField;
import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.jdbcResultSet;
import br.com.dreamsource.mobile.worders.WOrders;
import br.com.dreamsource.mobile.components.FormWOrders;
import br.com.dreamsource.mobile.worders.forms.core.Consts;
import br.com.dreamsource.mobile.worders.forms.core.DatabaseManager;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Elizeu Nogueira da Rosa Jr.
 * @version 0.1
 * @date 21.05.2007
 *
 */

public class CustomerEdit extends FormWOrders {
    
    private int editType;
    private jdbcResultSet resultSet;
    
    private DBChoiceGroup comboPriceTable;
    private String[] comboPriceTableItems;
    private Image[] comboPriceTableImage;
    
    private DBTextField tfName;
    private DBTextField tfCPF;
    private DBTextField tfCNPJ;
    private DBTextField tfPhone;
    private DBTextField tfFax;
    private DBTextField tfContact;
    private DBTextField tfMail;
    private DBTextField tfZipCode;
    private DBTextField tfCredit;
    
    public CustomerEdit(CommandListener listener, WOrders midlet, int editType, jdbcResultSet resultSet) {
        super(listener, midlet, "Produtos");
        this.resultSet = resultSet;
        this.editType = editType;
        try {
            
            this.loadPriceTable();
            if (this.editType == Consts.UPDATE_TYPE_INSERT) {
                this.setTitle("Inserir cliente");
            } else {
                this.resultSet.next();
                if (this.editType == Consts.UPDATE_TYPE_DELETE) {
                    this.setTitle("Excluir?");
                    this.disableControls();
                } else {
                    this.setTitle("Editar cliente");
                }
                //this.setTitle(this.editType == Consts.UPDATE_TYPE_DELETE ? "Excluir?" : "Editar cliente");
                this.setComponentValues(this.resultSet);
                this.comboPriceTable.setSelectedIndexByID(this.resultSet.getString(Consts.CUSTOMER_PRICETABLEID));
            }
        } catch (SQLException sqlEx) {
            this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this.midlet.getCustomer()));
            this.midlet.destroyForm(this);
        }
            
    }
    
    private DBTextField getTfName() {
        if (tfName == null) {
            tfName = new DBTextField("Nome(*)", "", 80, DBTextField.ANY, false);
        }
        return tfName;
    }        
    
    private DBTextField getTfCPF() {
        if (tfCPF == null) {
            tfCPF = new DBTextField("CPF", "", 11, DBTextField.ANY, true);
        }
        return tfCPF;
    }        
    
    private DBTextField getTfCNPJ() {
        if (tfCNPJ == null) {
            tfCNPJ = new DBTextField("CNPJ", "", 14, DBTextField.ANY, true);
        }
        return tfCNPJ;
    }        
    
    private DBTextField getTfPhone() {
        if (tfPhone == null) {
            tfPhone = new DBTextField("Fone(*)", "", 10, DBTextField.NUMERIC, false);
        }
        return tfPhone;
    }        
    
    private DBTextField getTfFax() {
        if (tfFax == null) {
            tfFax = new DBTextField("Fax", "", 10, DBTextField.NUMERIC, true);
        }
        return tfFax;
    }        
    
    private DBTextField getTfContact() {
        if (tfContact == null) {
            tfContact = new DBTextField("Contato(*)", "", 80, DBTextField.ANY, false);
        }
        return tfContact;
    }        
    
    private DBTextField getTfMail() {
        if (tfMail == null) {
            tfMail = new DBTextField("E-Mail", "", 80, DBTextField.EMAILADDR, true);
        }
        return tfMail;
    }        
    
    private DBTextField getTfZipCode() {
        if (tfZipCode == null) {
            tfZipCode = new DBTextField("CEP(*)", "", 8, DBTextField.ANY, false);
        }
        return tfZipCode;
    }        
    
    private DBTextField getTfCredit() {
        if (tfCredit == null) {
            tfCredit = new DBTextField("Credito(*)", "", 15, DBTextField.DECIMAL, false);
        }
        return tfCredit;
    }        
    
    private DBChoiceGroup getComboPriceTable() {
        if (comboPriceTable == null) {
            comboPriceTableItems = new String[]{};
            comboPriceTableImage = new Image[]{};
            comboPriceTable = new DBChoiceGroup("Tabela de preco(*)", Choice.POPUP, 
                    comboPriceTableItems, comboPriceTableImage);
            comboPriceTable.setSelectedFlags(new boolean[]{});
            comboPriceTable.setFitPolicy(Choice.TEXT_WRAP_ON);
        }
        return comboPriceTable;
    }       

    protected void createComponents() {
        this.append(this.getComboPriceTable());
        this.append(this.getTfName());
        this.append(this.getTfCPF());
        this.append(this.getTfCNPJ());
        this.append(this.getTfPhone());
        this.append(this.getTfFax());
        this.append(this.getTfContact());
        this.append(this.getTfMail());
        this.append(this.getTfZipCode());
        this.append(this.getTfCredit());
        this.addCommand(this.getSaveCommand());
        this.addCommand(this.getCancelCommand());
    }
    
    private void loadPriceTable() throws SQLException {
        DatabaseManager dbManager = new DatabaseManager(this.midlet.getUserPOJO());
        jdbcResultSet result = dbManager.list(Consts.PRICETABLE_SELECT_BY_EDIT);
        this.comboPriceTable.loadByResultSet(result, Consts.PRICETABLE_NAME, Consts.PRICETABLE_ID);
    }
    
    public void fireCommand(Command command) {
        if (command == this.saveCommand) {
            if (this.editType == Consts.UPDATE_TYPE_INSERT) {
                try {
                    this.processInsert(Consts.CUSTOMER_INSERT);
                    this.sendToForm(this.midlet.getSystemMessage("Sucesso", "Registro inserido com sucesso!", this.midlet.getCustomer()));
                    this.midlet.destroyForm(this);
                } catch (SQLException sqlEx) {
                    this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
                }
            } else 
            if (this.editType == Consts.UPDATE_TYPE_UPDATE) {
                try {
                    this.processUpdate(Consts.CUSTOMER_UPDATE);
                    this.sendToForm(this.midlet.getSystemMessage("Sucesso", "Registro atualizado com sucesso!", this.midlet.getCustomer()));
                    this.midlet.destroyForm(this);
                } catch (SQLException sqlEx) {
                    this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
                }
            } else {
                try {
                    this.processDelete(Consts.CUSTOMER_DELETE);
                    this.sendToForm(this.midlet.getSystemMessage("Sucesso", "Registro excluido com sucesso!", this.midlet.getCustomer()));
                    this.midlet.destroyForm(this);
                } catch (SQLException sqlEx) {
                    this.sendToForm(this.midlet.getSystemMessage("ERRO", sqlEx.getMessage(), this));
                }
            }
        } else
        if (command == this.cancelCommand) {
            this.sendToForm(this.midlet.getCustomer());
        } 
    }
    
}
